const app = getApp();
const util = require('../../utils/util.js')
var QQMapWX = require("../../libs/qqmap-wx-jssdk.js");
var qqmapsdk;
Page({
  data: {
    backPage: 1,
    inDeliveryScope: true
  },
  onLoad: function (options) {
    //请求分享数据
    wx.request({
      url: app.globalData.ApiUrl + '/index.php?act=wx_share&op=get_share',
      data: {},
      method: 'GET',
      dataType: 'json',
      success: res => {
        if (res.data.code == '200') {
          this.setData({
            share_default_title: res.data.datas.title,
            share_default_image_url: res.data.datas.imageUrl
          })
        }
      }
    })
    if (options.address_id) {
      this.setData({
        address_id: options.address_id
      })
    }
    if (options.backPage) {
      this.setData({
        backPage: options.backPage * 1
      })
    }
    util.share();
    var self = this;
    wx.getStorage({
      key: 'key',
      success: (res) => {
        self.setData({
          key: res.data,
        })
      }
    })
    var curPages = getCurrentPages();
    var prePages = curPages[curPages.length - 2];
    if (prePages != undefined) {
      this.setData({
        address_id: prePages.data.address_id
      })
    }
  },

  onReady: function () {
    var self = this;
    wx.request({
      url: app.globalData.ApiUrl + '/index.php?act=member_address&op=address_list',
      data: {
        key: self.data.key,
        chain_id: ''
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      success: (res) => {
        if (res.data.code == 200) {
          self.setData({
            address_list: res.data.datas.address_list
          })
        }
      }
    });
  },
  verifyDistance: function (index) {
    var self = this;
    qqmapsdk = new QQMapWX({
      key: 'FS4BZ-HINKW-7XBRR-OLZ5Y-PLPF6-5CF5J'
    });
    qqmapsdk.calculateDistance({
      mode: 'straight',
      from: {
        latitude: self.data.address_list[index].lat,
        longitude: self.data.address_list[index].lng
      },
      to: [{
        latitude: self.data.address_list[index].logistics.logistics_reference_lat,
        longitude: self.data.address_list[index].logistics.logistics_reference_lng
      }],
      success: ressuc => {
        if (ressuc.result.elements[0].distance - self.data.address_list[index].logistics.logistics_distance * 1000 > 0) {
          self.setData({
            inDeliveryScope: false
          });
        } else {
          self.setData({
            inDeliveryScope: true
          });
        }
      }
    });
  },
  changeAddr: function (e) {
    var addressid = e.currentTarget.dataset.addressid
    var index = e.currentTarget.dataset.index
    var curPages = getCurrentPages();
    var prePages;
    this.verifyDistance(index)
    if (!this.data.inDeliveryScope) {
      wx.showModal({
        title: '超出配送范围！',
        content: '是否查看可配送范围',
        success: res => {
          if (res.confirm) {
            wx.navigateTo({
              url: '../delivery_area/delivery_area'
            });
          }
        }
      });
      return false;
    }
    if (this.data.backPage - 1 == 0) {
      prePages = curPages[curPages.length - 2];
    }
    if (this.data.backPage - 1 > 0) {
      prePages = curPages[curPages.length - 4];
    }
    prePages.setData({
      address_id: addressid,
      address_info: this.data.address_list[index]
    })
    wx.navigateBack({
      delta: this.data.backPage
    });
  },
  editAddr: function (e) {
    let addressid = e.currentTarget.dataset.addressid;
    wx.redirectTo({
      url: '../address_add/address_add?addressid=' + addressid
    })
  },
  delAddr: function (e) {
    var self = this;
    wx.showModal({
      title: '删除地址',
      content: '确认删除吗？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_address&op=address_del',
            data: {
              key: self.data.key,
              address_id: e.currentTarget.dataset.addressid
            },
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            dataType: 'json',
            success: (res) => {
              if (res.data.code == 200) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 1000,
                  success: function () {
                    setTimeout(function () {
                      let address_list_new = self.data.address_list;
                      address_list_new.splice(e.currentTarget.dataset.index, 1);
                      self.setData({
                        address_list: address_list_new
                      })
                    }, 1000);
                  }
                })
              }
            }
          });
        }
      }
    })
  },
  onShareAppMessage: function () {
    let share_key = this.data.key ? this.data.key : '';
    let share_path = '';
    if (util.getCurrentPageUrlWithArgs().indexOf("?") != -1) {
      share_path = util.getCurrentPageUrlWithArgs() + '&share_key=' + share_key;
    } else {
      share_path = util.getCurrentPageUrlWithArgs() + '?share_key=' + share_key;
    }
    return {
      title: this.data.share_default_title,
      imageUrl: this.data.share_default_image_url,
      path: share_path
    }
  }
})